/**
 * 
 */
package org.gaming.prefab.thing.longn;

import org.gaming.db.repository.BaseRepository;
import org.gaming.prefab.IGameCause;
import org.gaming.prefab.thing.cost.CostReceipt;
import org.gaming.prefab.thing.reward.RewardReceipt;

/**
 * @author YY
 * 数值资源类的储存器
 */
public abstract class NumberThingStorer<Log extends NumberThingLog> extends LongnThingStorer<RewardReceipt<Long>, CostReceipt<Long>> {
	
	protected abstract Log newLog();
	protected abstract BaseRepository<Log> findLogRepository(int dbZone);
	
	@Override
	protected final void afterAdd(long entityId, int dbZone, RewardReceipt<Long> receipt, IGameCause cause, String remark) {
		String ownerName = this.getOwnerName(entityId);
		BaseRepository<Log> repository = findLogRepository(dbZone);
		
		for(RewardReceipt<Long>.RewardDetail detail : receipt.getDetails()) {
			Log log = newLog();
			log.setOwnerId(entityId);
			log.setOwnerName(ownerName);
			log.setCurr(this.getCount(entityId, detail.getThingId()));
			log.setDelta(detail.getNum().longValue());
			log.setGameCause(cause);
			log.setThingId(detail.getThingId());
			log.setThingName(thingName(detail.getThingId()));
			log.setRemark(remark + receipt.getRemark());
			repository.insert(log);
		}
	}
	
	@Override
	protected final void afterCost(long entityId, int dbZone, CostReceipt<Long> receipt, IGameCause cause, String remark) {
		String ownerName = this.getOwnerName(entityId);
		BaseRepository<Log> repository = findLogRepository(dbZone);
		
		for(CostReceipt<Long>.CostDetail detail : receipt.getDetails()) {
			Log log = newLog();
			log.setOwnerId(entityId);
			log.setOwnerName(ownerName);
			log.setCurr(this.getCount(entityId, detail.getThingId()));
			log.setDelta(-detail.getNum().longValue());
			log.setGameCause(cause);
			log.setThingId(detail.getThingId());
			log.setThingName(thingName(detail.getThingId()));
			log.setRemark(remark);
			repository.insert(log);
		}
	}
	
	protected String thingName(int thingId) {
		return "";
	}
}
